define(function(require, exports, module) {
    "use strict";
    var $ = require("$"),
        app = require("dist/application/app"),
        config = app.config;

    exports.init = function($form) {
        var is_modal = $form.hasClass("form-modal");
        var modal = null;
        if (is_modal) {
            modal = $form.parents(".modal");
            modal.on('hidden', function() {
                $form.resetForm()
            })
        };
        var vdefault = {
            errorElement: "span",
            errorClass: "help-block error",
            errorPlacement: function(a, e) {
                // e.parents("label").length > 0 ? e.parents("label").after(a) : e.after(a)
                var g = e.parents(".input-group");
                g.length > 0 ? g.after(a) : e.after(a)
            },
            highlight: function(a) {
                // $(a).closest(".form-group").removeClass("has-error has-success").addClass("has-error")
                $(a).removeClass("error has-success").addClass("error");
            },
            success: function(a) {
                // a.addClass("valid").closest(".form-group").removeClass("has-error has-success")
                a.addClass("valid")
            },
            onkeyup: function(a) {
                $(a).valid()
            },
            onfocusout: function(a) {
                $(a).valid()
            },
            submitHandler: function(form) {
                var _sb = true;
                if ($('[data-toggle="kindeditor"]',form).length>0) {
                    if (typeof KindEditor !== "undefined" && KindEditor.instances) {
                        $.each(KindEditor.instances, function() {
                        this.sync();
                        var $element = $(this.srcElement[0]);
                        var r = $element.data("ruleRequired"),
                            m = $element.data("msgRequired"),
                            e = $element.data("ruleRangelength"),
                            em = $element.data("msgRangelength");
                        var v = $.trim($element.val()).replace(/(&nbsp;)|\s|\u00a0/g, '');
                        if (r) {
                            if (v.length == 0) {
                                var msg = m;
                                config.msg.error(msg || "内容不能为空")
                                _sb = false;
                                return false
                            }
                        };
                        if (e) {
                            var min = e[0];
                            var max = e[1];
                            var vv = $element.val();
                            var len=this.count('text');
                            if (len < min || len > max) {
                                config.msg.error(em || "内容不能小于{0}且大于{1}".format(min, max))
                                _sb = false;
                                return false
                            }
                        }
                    })
                }};
                if (_sb) {
                    var btn = $("button[type='submit']", form);
                    btn.button('loading');
                    $(form).ajaxSubmit({
                        dataType: 'json',
                        success: function(d) {
                            btn.button('reset')
                            if (modal) modal.modal('hide');
                            tip.run(d)
                        },
                        error: function(d) {
                            btn.button('reset')
                            if (modal) modal.modal('hide')
                            config.msg.msgbox.alert(d.responseText)
                        }
                    })
                }
            }
        }

        var ignore = $form.data("ignore")
        if (ignore) {
            vdefault.ignore = ignore
        };
        var $errorContainer = $("div.errorContainer")
        var errorContainer = {
            errorContainer: $("div.errorContainer"),
            errorLabelContainer: $("div.errorLabelContainer"),
            errorElement: "label"
        }
        $errorContainer.length > 0 && $.extend(vdefault, errorContainer);

        require.async(['validate', 'jform'], function() {
            $form.each(function() {
                $(this).validate(vdefault);
            })
        })
    }
    var tip = require("./tip");


})
